var app = angular.module("app",[]);
app.config(function($httpProvider){
    $httpProvider.defaults.transformRequest=function(obj){
        var str=[];
        for(var p in obj){
            str.push(encodeURIComponent(p)+"="+encodeURIComponent(obj[p]));
        }
        return str.join("&");
    };
    $httpProvider.defaults.headers.post={
        'Content-Type':'application/x-www-form-urlencoded'
    }
})

app.controller("personal_infoController",function($scope,$http){

    
	$scope.initView= function(uid){
		$http({
			method:'post',
			url:webRoot+'?s=/App/Username/getUserInfo',
			data:{
				uid:uid,
			}
		}).then(function successCallback(response){
			console.log(JSON.stringify(response));         
			$scope.info = response.data.data//最新内容数据 
			
		},function errorfunction(e){
			console.log(JSON.stringify(e));
		})	
 	}
});
document.addEventListener('plusready',function(){
	appElement=document.querySelector('[ng-controller=personal_infoController]');
	$scope= angular.element(appElement).scope();
	var uid = plus.storage.getItem('id');
    $scope.initView(uid);
	$scope.$apply();
	
	var id = plus.storage.getItem('id');
//	var nickname = plus.storage.getItem('nickname');
//	var photo = plus.storage.getItem('photo');
//	var tel = plus.storage.getItem('tel');
	var account = plus.storage.getItem('account');
//	var sex = plus.storage.getItem('sex');
//	$("#j_phone").val(account);
//	$("#j_nickname").val(nickname);
//	$("#j_photo").attr('src',photo);
	
//	if(sex=="1"){
//		$("#opensheet").html('男');
//	}else{
//		$("#opensheet").html('女');
//	}
	
	//选择性别
	$('.sex').on('tap',function(){
		$('#opensheet').html($(this).html());
	});
	
	$('#uPic').on('tap', function() {
		plus.nativeUI.actionSheet({ cancel: "取消", buttons: [{ title: "从相册选择" },{ title: "拍照上传" }] }, function(e) {
			if(e.index == 2) {
				var car = plus.camera.getCamera();
				car.captureImage(function(path) {
					path = plus.io.convertLocalFileSystemURL(path);
					$('#uPic img').attr('src', path);
					appendPic('file://'+path);
				}, function(err) {});

			} else if(e.index == 1) {
				plus.gallery.pick(function(path) {
					$('#uPic img').attr('src', path);
					appendPic(path);
				}, function(err) {});
			}
		})
	})
	
	var index = 1;
	var files = [];
	// 添加照片
	function appendPic(p) {
		files = {name:"uploadkey"+index,path:p};
//		index++;
//		upload();
	}
	function upload(){
		plus.nativeUI.showWaiting("上传中...");
		var server = webRoot+'?s=/App/Username/personalUpdate';
		var task = plus.uploader.createUpload(server, {
				method: "post",
				blocksize: 102400,
				timeout: 0
			},
			function(t, status) { //上传完成
				plus.nativeUI.closeWaiting();
				if(status == 200) {
					var infopics = $.parseJSON(t.responseText);
					console.log(t.responseText);
					plus.storage.setItem('icon',infopics.data);
					
					if(t.responseText.data != '') {
						plus.webview.currentWebview().close();
					}
				} else {
					toast('上传失败：' + status);
					plus.nativeUI.closeWaiting();
				}
			},
			function() {
				plus.nativeUI.toast('上传出错!');
				plus.nativeUI.closeWaiting();
			}
		);
		task.addFile(files.path,{key:"icon"});
		task.start();
		
	}
	//填写完毕后  触发保存按钮
	$('#save_btn').on('tap',function(){
		if(files.length!=0){
			upload();
		}
		mui.ajax(webRoot+'?s=/App/Username/personalUpdate',{
			type:"post",
			data:{
				account:account,
				nickname:$("#j_nickname").val(),
				sex:$("#opensheet").html(),
				tel:$("#j_phone").val(),
			},
			success:function(data){
				code = $.parseJSON(data);
				if(code.code == 200){
					plus.storage.setItem('photo',code.data.photo);//头像
					plus.storage.setItem('nickname',code.data.nickname+'');//昵称
					plus.storage.setItem('sex',code.data.sex.toString());//性别	
					plus.storage.setItem('tel',code.data.tel);//手机
//					var sex = plus.storage.getItem('sex');
//					if(sex=="1"){
//						$("#opensheet").html('男');
//					}else{
//						$("#opensheet").html('女');
//					}
					mui.toast('保存成功');
				}
				if(code.code == 201){
					mui.toast('修改失败');
					
				}
			},
			error:function(data){
				console.log(data);
			}
		});
		setTimeout(function(){
			plus.webview.currentWebview().close();
		},1000);
	})
})
		